package com.southwind.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.southwind.common.Cache;
import com.southwind.common.LngLat;
import com.southwind.common.Rectangle;
import com.southwind.common.ResultObj;
import com.southwind.entity.Admin;
import com.southwind.entity.Area;
import com.southwind.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-04-28
 */
@RestController
@RequestMapping("/area")
public class AreaController {

    @Autowired
    private AreaService areaService;

    @GetMapping("/list")
    public List<Area> list(){
        return this.areaService.list();
    }

    @GetMapping("/add")
    public ResultObj add(Area area){
        Admin admin = (Admin) Cache.map.get("user");
        area.setAid(admin.getId());
        boolean save = this.areaService.save(area);
        if(!save) return ResultObj.AREA_ADD_ERROR;
        return ResultObj.AREA_ADD_SUCCESS;
    }

    @RequestMapping("/show")
    @ResponseBody
    public List<Rectangle> show(HttpSession session){
        List<Area> list = this.areaService.list();
        List<Rectangle> result = new ArrayList<>();
        for (Area area : list) {
            Rectangle rectangle = new Rectangle();
            LngLat left = new LngLat();
            left.setLng(area.getLeftlng().toString());
            left.setLat(area.getLeftlat().toString());
            LngLat right = new LngLat();
            right.setLng(area.getRightlng().toString());
            right.setLat(area.getRightlat().toString());
            rectangle.setLeft(left);
            rectangle.setRight(right);
            rectangle.setName(area.getArea());
            rectangle.setPrice(area.getPrice());
            result.add(rectangle);
        }
        return result;
    }

}

